package org.jeecg.modules.demo.spcFlow.service.impl;

import org.jeecg.modules.demo.spcFlow.entity.SpcFlowMaster;
import org.jeecg.modules.demo.spcFlow.entity.SpcFlowMasterDetail;
import org.jeecg.modules.demo.spcFlow.mapper.SpcFlowMasterDetailMapper;
import org.jeecg.modules.demo.spcFlow.mapper.SpcFlowMasterMapper;
import org.jeecg.modules.demo.spcFlow.service.ISpcFlowMasterService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: spc_flow_master
 * @Author: jeecg-boot
 * @Date:   2023-12-14
 * @Version: V1.0
 */
@Service
public class SpcFlowMasterServiceImpl extends ServiceImpl<SpcFlowMasterMapper, SpcFlowMaster> implements ISpcFlowMasterService {

	@Resource
	private SpcFlowMasterMapper spcFlowMasterMapper;
	@Resource
	private SpcFlowMasterDetailMapper spcFlowMasterDetailMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(SpcFlowMaster spcFlowMaster, List<SpcFlowMasterDetail> spcFlowMasterDetailList) {
		spcFlowMasterMapper.insert(spcFlowMaster);
		if(spcFlowMasterDetailList!=null && spcFlowMasterDetailList.size()>0) {
			for(SpcFlowMasterDetail entity:spcFlowMasterDetailList) {
				//外键设置
				entity.setFlowNo(spcFlowMaster.getId());
				spcFlowMasterDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(SpcFlowMaster spcFlowMaster,List<SpcFlowMasterDetail> spcFlowMasterDetailList) {
		spcFlowMasterMapper.updateById(spcFlowMaster);

		//1.先删除子表数据
		spcFlowMasterDetailMapper.deleteByMainId(spcFlowMaster.getId());

		//2.子表数据重新插入
		if(spcFlowMasterDetailList!=null && spcFlowMasterDetailList.size()>0) {
			for(SpcFlowMasterDetail entity:spcFlowMasterDetailList) {
				//外键设置
				entity.setFlowNo(spcFlowMaster.getId());
				spcFlowMasterDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		spcFlowMasterDetailMapper.deleteByMainId(id);
		spcFlowMasterMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			spcFlowMasterDetailMapper.deleteByMainId(id.toString());
			spcFlowMasterMapper.deleteById(id);
		}
	}

}
